{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scapy.all import *\n",
    "from scapy.all import (\n",
    "    TunTapInterface,\n",
    "    Ether,\n",
    "    IP,\n",
    "    ICMP,\n",
    ")\n",
    "\n",
    "from net import (\n",
    "    get_tap,\n",
    "    rmac,\n",
    "    lmac,\n",
    "    hmac,\n",
    "    broadcast,\n",
    "    laddr,\n",
    "    raddr,\n",
    "    haddr\n",
    ")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<https://scapy.readthedocs.io/en/latest/api/scapy.layers.html>\n",
    "\n",
    "<https://scapy.readthedocs.io/en/latest/api/scapy.layers.inet.html>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "tap = get_tap(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试 ICMP Echo Reply\n",
    "\n",
    "pkt = Ether(dst=lmac, src=rmac)\n",
    "pkt /= IP(dst=laddr, src=raddr,)\n",
    "pkt /= ICMP(type=0)\n",
    "pkt /= \"hello icmp\"\n",
    "tap.send(pkt)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 测试 ICMP Echo\n",
    "\n",
    "pkt = Ether(dst=hmac, src=lmac)\n",
    "pkt /= IP(dst=haddr, src=laddr)\n",
    "pkt /= ICMP(type=8, id=1, seq=1)\n",
    "pkt /= \"hello icmp\"\n",
    "tap.send(pkt)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "test",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
